package com.lily.aidouzixun.interfacez;

import android.app.Activity;
import android.content.Intent;
import android.webkit.JavascriptInterface;

import com.lily.aidouzixun.activity.CommentActivity;
import com.lily.aidouzixun.activity.LoginActivity;
import com.lily.aidouzixun.activity.ShowImageActivity;
import com.lily.aidouzixun.util.MyConfig;
import com.lily.aidouzixun.util.MyConstant;
import com.lily.aidouzixun.util.Validator;

/**
 * 与WebView通信接口
 */
public final class DetailJsInterface {
    private Activity mActivity;
    public DetailJsInterface(Activity mActivity){
        this.mActivity = mActivity;
    }
    /**
     * Android 4.2 以后必须加@JavascriptInterface否则会报错
     * 报错信息：E/Web Console﹕ Uncaught TypeError: Object [object Object] has no method 'comment' at xxx
     */
    @JavascriptInterface
    public void comment(String  ldid) {
        if (!Validator.isEffective(MyConfig.getUserId(mActivity))) {  //没有登录,跳转登陆页
            Intent intent = new Intent(mActivity, LoginActivity.class);
            intent.putExtra(MyConstant.IntentKey.BACK_TITLE, "返回");
            mActivity.startActivityForResult(intent, MyConstant.IntentForResultCODE.LOGIN_REQUESTCODE);

        } else {    //登录了,跳转评论页
            Intent commentIntent = new Intent(mActivity, CommentActivity.class);
            commentIntent.putExtra(MyConstant.IntentKey.LDID, ldid);
            commentIntent.putExtra(MyConstant.IntentKey.COMMENT_FROM, CommentActivity.FromType.LONGDISTANCE_DETAIL);
            mActivity.startActivityForResult(commentIntent, MyConstant.IntentForResultCODE.LONGDISTANCE_COMMENT_REQUESTCODE);
        }
    }

    /**
     * 显示图片
     * Android 4.2 以后必须加@JavascriptInterface否则会报错
     * 报错信息：E/Web Console﹕ Uncaught TypeError: Object [object Object] has no method 'comment' at xxx
     */
    @JavascriptInterface
    public void showImage(String imageUrl) {
        Intent intent = new Intent(mActivity, ShowImageActivity.class);
        intent.putExtra(MyConstant.IntentKey.IMAGE_FROM, ShowImageActivity.ImageFrom.URL);
        intent.putExtra(MyConstant.IntentKey.IMAGE_URL, imageUrl);
        mActivity.startActivity(intent);
    }

}
